<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>成库</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    <style type="text/css">
        #taskList {
            border: 1px solid #959595;
        }
        #taskList tr th{
            background-color: #6ecac5;
        }
        #taskList tr th, #taskList tr td {
            border: 1px #d6d6d6 dashed;
            font-size: 15px;
            text-align: center;
            height: 30px;
        }

    </style>
</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border: none;">
        <div class="nav" id="nav"></div>
        <div class="easyui-panel" style="border:none;">
            <form id="ff" method="post">
                <div style="margin-bottom: 20px;">
                    <label class="item-label">年：</label>
                    <input type="text" id="year" name="year" class="easyui-combobox" data-options="required:true" required="required" style="width: 100px;"/>
                    <span id="monthLabel">
                        <label class="item-label">月：</label>
                        <input type="text" id="month" name="month" class="easyui-combobox" data-options="required:true" required="required" style="width: 100px;" >
                        </span>
                    <span id="weekLabel">
                        <label class="item-label">周：</label>
                        <input type="text" id="week" name="week" class="easyui-combobox" data-options="required:true" required="required" style="width: 100px;" >
                            </span>
                    <label class="item-label">品类：</label>
                    <select id="categorySelect" name="categoryCode" class="easyui-combobox" data-options="required:true" required="required" style="width:100px;">
                    </select>
                    <a href="javascript:void(0);" class="easyui-linkbutton c6" onclick="statistics()" style="width:80px;margin-left: 40px;">成库</a>
                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="searchBy()" style="width:80px;margin-left: 40px;">查询</a>
                    <input id="taskListCategoryCodes" style="display: none" />
                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="taskList()" style="width:80px;margin-left: 40px;">任务列表</a>
                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="skuCon()" style="width:80px;margin-left: 40px;" id="skuBut">确认sku</a>
                    <!--<a href="javascript:void(0);" class="easyui-linkbutton c6" onclick="synCombo()" style="width:80px;margin-left: 40px;" id="comboBut">同步套餐</a>-->
                </div>
            </form>
        </div>
        <div class="table-wrapper">
            <table id="datagrid"  style="height: 715px" data-options="singleSelect:true,fitColumns:true,scrollbarSize:0,checkOnSelect:false">
                <thead>
                <tr>
                    <th data-options="field:'categoryName',align:'center'" width="120">品类</th>
                    <th data-options="field:'year',align:'center'" width="80">年</th>
                    <th data-options="field:'month',align:'center'" width="80">月</th>
                    <th data-options="field:'week',align:'center'" width="80">周</th>
                    <th data-options="field:'updateTime',align:'center'" width="200">成库时间</th>
                    <th data-options="field:'dataCount',align:'center'" width="150">数据条数</th>
                    <th data-options="field:'operateUser',align:'center'" width="200">上传人</th>
                    <th data-options="field:'status',align:'center',formatter:statisticsStatusFormatter" width="150">报告状态</th>
                    <th data-options="field:'sinkStatus',align:'center',formatter:sinkStatusFormatter" width="200">成库状态</th>
                    <th data-options="field:'action',formatter:actions,align:'center'" width="330">操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
    <div class="easyui-panel" style="border: none;"></div>
    <div id="taskListText" class="easyui-window upload-wrapper" title="任务列表" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:800px;height: 600px;padding:10px;">
        <input type="button" class="easyui-linkbutton" onclick="refreshTaskList()" value="刷新" style="height: 32px;width:60px;background:#cde5fd"/>
        <table id="taskList" style="width: 100%;" cellspacing="0" ></table>
    </div>
</div>
<div id="m" class="easyui-window" title="型号禁用" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:200px;height:280px;padding:10px;">
    <div >
        <ul id="result">

        </ul>
    </div>
</div>
</body>
<script>
    var skuConfirm = false;

    $(function () {
        let reportType = window.parent.workMode.toUpperCase();
        if(reportType =="ONLINE"){
            $("#skuBut").show();
        }else{
            $("#skuBut").hide();
        }

        // $("#comboBut").next().hide();
        setNav();
        $("#datagrid").datagrid({
            pagination: true,
            rownumbers: true,
            checkOnSelect: false
        }).datagrid("getPager").pagination({
            onSelectPage:searchBy
        });
        $.when(initMonthWeek("monthLabel","weekLabel"),initCategorySelect("categorySelect")).done(function () {
            searchBy(1,100);
        });
        aRecords("成库详情","/webapp/pages/product/statistics")
    });

    function initCategorySelect(selectId){
        var dtd = $.Deferred();
        var data = window.parent.userCategoryManageList;
        if(data) {
            var category = new Array(data.length + 1);
            var codes = "";
            for (let i = 0; i < data.length; i++) {
                category[i + 1] = data[i];
                codes += data[i].code + ",";
            }
            category[0] = {"name": "--全部--", "code": codes}
            $("#taskListCategoryCodes").val(codes); //用于查询成库任务列表
            $("#" + selectId).combobox({
                "textField": "name",
                "valueField": "code",
                "panelHeight": 150,
                "data": category,
            }).combobox("setValue", category[0]);
        }
        dtd.resolve();
        return dtd.promise();
    }

    function statistics() {
        var code = $("#categorySelect").combobox('getValue');
        if (code.indexOf(",")>=0) {
            $.messager.alert("提示", "请选择成库品类","info");
            return;
        }

        let reportType = window.parent.workMode.toUpperCase();
        if (reportType == "ONLINE" && !skuConfirm) {
            $.messager.alert("提示", "请确认sku", "info");
            return;
        }

        $.messager.confirm("提示","您确定成库吗？",function(flag) {
            if (flag) {
                var param = getFormData(document.getElementById("ff"));
                param.reportType = window.parent.workMode.toUpperCase();
                $.ajax({
                    url: config.url + "/salesStatistics/statistics",
                    data: param,
                    type: "get",
                    xhrFields: {withCredentials: true},
                    beforeSend: function () {
                        var valid = $("#ff").form('enableValidation').form('validate');
                        if (!valid) return false;
                        $.messager.progress({
                            title: "请稍等",
                            interval: 50,
                            text: "数据加载中..."
                        });
                        return true;
                    },
                    success: function (data) {
                        $.messager.progress("close");
                        if(data.result=="true"){
                            $.messager.alert("提示", "成库成功","info");
                        }else{
                            var str="成库失败:</br>";
                            if (data.mall){
                                str+="存在"+data.mall+"个商场为空/禁用；"
                            }
                            if (data.model){
                                str+="存在"+data.model+"个型号为空；"
                            }
                            if (data.brand){
                                str+="存在"+data.brand+"个品牌为空；"
                            }
                            if (data.price){
                                str+="存在"+data.price+"个价格小于15；"
                            }
                            if (data.count){
                                str+="存在"+data.count+"个数量为0；</br>"
                            }
                            if (data.disable){
                                str += "禁用型号" + data.disable + ";</br>";
                            }
                            if (data.unknown) {
                                str += "未知型号共" + data.unknown + "条</br>";
                            }
                            if (data.unconfirmedModel) {
                                str += "当期含有不确定型号" + data.unconfirmedModel+";</br>";
                            }
                            if (data.unsplit) {
                                str += data.unsplit+";</br>";
                            }
                            if (data.otherName) {
                                str += data.otherName+";</br>";
                            }
                            if (data.unreleased) {
                                str += data.unreleased+";</br>";
                            }
                            if (data.tjCheck) {
                                str += data.tjCheck;
                            }
                            if (data.channel) {
                                str += data.channel+";</br>";
                            }
                            if (data.wait) {
                                str += data.wait+";</br>";
                            }
                            $.messager.alert("错误", str).window({ width: 350, height: 300 });;
                        }
                    },
                    error: function () {
                        $.messager.progress("close");
                        $.messager.alert("提示", "成库失败");
                    }
                })
            }
        });
    }

    function searchBy(page,size){
        var data = getFormData(document.getElementById("ff"));
        data['page'] = page||1;
        data['size'] = size||100;
        data.reportType = window.parent.workMode.toUpperCase();
        $.ajax({
            url:config.url + "/salesStatistics/page",
            type:"get",
            data:transfer(data),
            xhrFields:{withCredentials:true},
            beforeSend:function () {
              $.messager.progress({
                  title:"请稍等",
                  interval:50,
                  text:"数据加载中..."
              });
            },
            complete:function () {
              $.messager.progress("close");
            },
            success:function(data){
                $.messager.progress("close");
                if(data){
                    $("#datagrid").datagrid("loadData",data);
                }
            },
            error:function(data){
                $.messager.progress("close");
                console.log(data);
            }
        });
    }

    function statisticsStatusFormatter(val,row) {
        var ss="";
        switch (val) {
            case "WAREHOUSE":
                ss="成库";
                break;
            case "TRANSFER":
                ss="转移数据";
                break;
            case "GENERATE":
                ss="生成报告";
                break;
            case "GENERATING":
                ss="报告生成中";
                break;
            case "DOWNLOAD":
                ss="下载报告";
                break;
        }
        return ss;
    }

    function sinkStatusFormatter(val, row) {
        var value="";
        switch (val) {
            case "WAITING":
                value="等待中";
                break;
            case "STATISTICS":
                value="统计中";
                break;
            case "RUNNING":
                value="同步中";
                break;
            case "COMBO_RUNNING":
                value="套餐同步中";
                break;
            case "UNCOMBO_RUNNING":
                value="非套餐同步中";
                break;
            case "STA_FAIL":
                value="统计失败";
                break;
            case "SYN_FAIL":
                value="同步失败";
                break;
            case "VISIT_FAIL":
                value="访问失败";
                break;
            case "SUCCESS":
                value="完成";
                break;
            case "STA_SUCCESS":
                value="统计完成";
                break;
        }
        return value;
    }

    function actions(val, row) {
        var ss = "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
            "style='width:100px;'  onclick='modelDisable(" + row.year + "," + row.month + "," + row.week + ",\"" + row.categoryCode + "\",\"" + row.reportType + "\")'>查看型号禁用</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
            "style='width:100px;'  onclick='againSyn(\"" + row.id + "\")'>重新同步</a>";
        if ("DOWNLOAD"!=row.status){
            return ss;
        }
        return ss+="<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' "+
            "style='width:80px;' onclick=''>下载报告</a>"
    }
    function modelDisable(year,month,week,categoryCode) {
        $.ajax({
            url:config.url+"/sales/detail/modelDisabled",
            type:"get",
            data:{"year":year,"month":month,"week":week,"categoryCode":categoryCode},
            xhrFields:{withCredentials:true},
            success:function (data) {
                $("#m").window("open");
                var html="";
                if(data&&data.length>0){
                    $.each(data,function (i, m) {
                        html+='<li>'+m+'</li>';
                    })
                }else {
                    html+='<li>无禁用型号</li>';
                }
                $("#result").html(html);
            }
        })
    }

    function againSyn(id) {
        $.messager.confirm("提示","您确定重新同步吗？",function(flag) {
            if (flag) {
                $.ajax({
                    url:config.url+"/salesStatistics/againSyn/"+id,
                    type:"get",
                    dataType: "json",
                    xhrFields:{withCredentials:true},
                    beforeSend:function () {
                        $.messager.progress({
                            title:"请稍等",
                            interval:20,
                            text:"操作进行中..."
                        });
                    },
                    success:function (data) {
                        $.messager.progress("close");
                        if (data.code == 200) {
                            $.messager.alert("提示", data.msg,"info");
                        }else{
                            $.messager.alert("提示", data.msg,"error");
                        }
                    }
                })
            }
        });

    }

    function taskList() {
        var categoryCodes = $("#taskListCategoryCodes").val();

        $.ajax({
            url: config.url + "/salesStatistics/taskList",
            type: "GET",
            data: {categoryCodes:categoryCodes},
            xhrFields: {withCredentials: true},
            beforeSend: function () {
                $.messager.progress({
                    title: "请稍等",
                    interval: 30,
                    text: "操作进行中..."
                });
            },
            complete: function () {
                $.messager.progress("close");
            },
            success: function (message) {
                $.messager.progress("close");
                if (message.code == 200) {
                    document.getElementById("taskList").innerHTML = message.msg;
                    $("#taskListText").window("open");
                } else {
                    $.messager.alert("提示", message.msg, "error");
                }
            },
            error: function () {
                $.messager.progress("close");
            }
        })
    }

    function refreshTaskList() {
        $("#taskListText").window("close");
        taskList();
    }

    // function synCombo() {
    //     var data = {};
    //     data.reportType = window.parent.workMode.toUpperCase();
    //     data.year = $("#year").textbox("getValue");
    //     if (isMonthType()) {
    //         data.days = $("#month").textbox("getValue");
    //     }else {
    //         data.days = $("#week").textbox("getValue");
    //     }
    //     data.access_token = e;
    //     $.messager.confirm("提示", "您确定要同步套餐表吗(包括非套餐)吗?", function (r) {
    //         if (r) {
    //             $.ajax({
    //                 url: window.config.synHost+"combo_ncombo",
    //                 type: "post",
    //                 data: data,
    //                 dataType: "json",
    //                 beforeSend: function () {
    //                     $.messager.progress({
    //                         title: "请稍等",
    //                         interval: 30,
    //                         text: "套餐数据同步中..."
    //                     });
    //                 },
    //                 success: function (data) {
    //                     $.messager.show({
    //                         title:'提示',
    //                         msg:data.msg,
    //                         timeout:0,
    //                         showType:'slide'
    //                     });
    //                     $.messager.progress("close");
    //                 },
    //                 error: function () {
    //                     $.messager.progress("close");
    //                     $.messager.alert("失败", "系统异常", "error");
    //                 }
    //             });
    //         }
    //     });
    // }

    $("#categorySelect").combobox({
        onChange:function () {
            var value = $("#categorySelect").combobox("getValue");
            //同步套餐按钮
            // if(value=="08"){
            //     $("#comboBut").show();
            // }else{
            //     $("#comboBut").hide();
            // }
            //确认sku按钮
            let reportType = window.parent.workMode.toUpperCase();
            if (reportType == "ONLINE") {
                skuConfirm = false;
            }

        }
    });

    $("#ff").keydown(function (event) {
        var e = event || window.event;
        var keyCode = e.keyCode || e.which;
        if(keyCode=="13"){
            searchBy();
        }
    });

    function skuCon() {
        var code = $("#categorySelect").combobox('getValue');
        if (code.indexOf(",")>=0) {
            $.messager.alert("提示", "请选择成库品类","info");
            return;
        }
        skuConfirm = true;
        $.messager.alert("提示", "sku已确认，可以进行成库", "info");
    }
</script>
</html>